import {
    GetOrderList,
    SetOrderTag,
    SetFinishOrder,
    CancelOrder,
    FindOrderItemFeeByOrderNo,
    ModifiedFee,
    GetOrderTag,
    GetTaskName,
    ExportOrderList,
    GetTaskList
} from '@/services/order/orderList';
import {catchException} from '@/utils/utils';
export default {
    namespace: 'orderList',

    state: {
        orderList: [],
        orderNoItem: [],
        orderTag: [],
        taskName: [],
        taskList:[]
    },

    effects: {

        // 获取订单列表
        *GetOrderList({payload, callBack}, {call, put}) {
            const data = yield call(GetOrderList, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    yield put({
                        type: 'saveGetOrderList',
                        payload: {
                            dataList: data.Data,
                        },
                    });
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
              })  
            
        },

          // 获取备注
          *GetOrderTag({payload, callBack}, {call, put}) {
            const data = yield call(GetOrderTag, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    yield put({
                        type: 'saveGetOrderTag',
                        payload: {
                            dataList: data.Data,
                        },
                    });
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
              })              
        },

             // 获取今日剩余导出次数和任务名称
             *GetTaskName({payload, callBack}, {call, put}) {
                const data = yield call(GetTaskName, payload);
                yield catchException(function* (){
                    if(data.Code === 0) {
                        yield put({
                            type: 'saveGetTaskName',
                            payload: {
                                dataList: data.Data,
                            },
                        });
                        callBack && callBack.success(data);
                    } else {
                        callBack && callBack.error(data)
                    }
                  })                  
            },

        // 获取调价页商品信息
        *FindOrderItemFeeByOrderNo({payload, callBack}, {call, put}) {
            const data = yield call(FindOrderItemFeeByOrderNo, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    yield put({
                        type: 'saveFindOrderItemFeeByOrderNo',
                        payload: {
                            dataList: data.Data,
                        },
                    });
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
              })              
        },

        // 获取导出任务列表
        *GetTaskList({payload, callBack}, {call, put}) {
            const data = yield call(GetTaskList, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    yield put({
                        type: 'saveGetTaskList',
                        payload: {
                            dataList: data.Data,
                        },
                    });
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
              })              
        },

        //修改备注
        *SetOrderTag({payload, callBack}, {call, put}) {
            const data = yield call(SetOrderTag, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
              })             
          },

        //确认收货
        *SetFinishOrder({payload, callBack}, {call, put}) {
            const data = yield call(SetFinishOrder, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
              })              
          },

        //取消订单
        *CancelOrder({payload, callBack}, {call, put}) {
            const data = yield call(CancelOrder, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
              })              
          },

          //保存调价信息
        *ModifiedFee({payload, callBack}, {call, put}) {
            const data = yield call(ModifiedFee, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
              })              
          },

        //导出
        *ExportOrderList({payload, callBack}, {call, put}) {
            const data = yield call(ExportOrderList, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
              })              
          },


    },

    reducers: {
        saveGetOrderList(state, {payload}) {
            return {
                ...state,
                orderList: payload.dataList,
            };
        },
        saveFindOrderItemFeeByOrderNo(state, {payload}) {
            return {
                ...state,
                orderNoItem: payload.dataList,
            };
        },
        saveGetOrderTag(state, {payload}) {
            return {
                ...state,
                orderTag: payload.dataList,
            };
        },
        saveGetTaskName(state, {payload}) {
            return {
                ...state,
                taskName: payload.dataList,
            };
        },
        saveGetTaskList(state, {payload}) {
            return {
                ...state,
                taskList: payload.dataList,
            };
        },
    }
}
